package com.lovo.bbs.controller;

import com.lovo.bbs.bean.Post;
import com.lovo.bbs.bean.User;
import com.lovo.bbs.bean.form.PostForm;
import com.lovo.bbs.result.ResponseResult;
import com.lovo.bbs.result.UserValid;
import com.lovo.bbs.service.PostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/posts")
@Tag(name = "帖子控制器PostController")
@UserValid
@SecurityRequirement(name = "bearer-key")
public class PostController {
    @Autowired
    private PostService postService;
    @GetMapping
    @Operation(summary = "查看所有帖子")
    @ResponseResult
    public Object listPosts(int current,int pageSize){
        return postService.listPosts(current,pageSize);
    }

    @PostMapping
    @ResponseResult
    public Object save(HttpServletRequest request, @RequestBody PostForm postForm){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Post post = new Post();
        BeanUtils.copyProperties(postForm,post);
        post.setUserId(user.getId());
        postService.save(post);
        return post;
    }

    @GetMapping("/{id}")
    @ResponseResult
    public Object getById(@PathVariable int id){
        // 根据id查询帖子，包含所有回帖
        return null;
    }
}
